package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateProductionPlan;
import com.ruoyi.mateproduction.domain.MateProductionPlanModel;
import com.ruoyi.mateproduction.mapper.MtProductionMapper;
import com.ruoyi.mateproduction.service.MtProductionService;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.product.domain.ProductModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MtProductionServiceImpl implements MtProductionService {

    @Autowired
    MtProductionMapper mtProductionMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Override
    public int addMatePlan(MateProductionPlan mateProductionPlan) {
        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = mtProductionMapper.addMatePlan(mateProductionPlan);

            if (mateProductionPlan.getMtPlanModels() != null) {

                for (MateProductionPlanModel mateProductionPlanModel : mateProductionPlan.getMtPlanModels()) {
                    mateProductionPlanModel.setDeptId(SecurityUtils.getDeptId());
                    mateProductionPlanModel.setPlanId(mateProductionPlan.getPlanId());
                    mtProductionMapper.addMatePlanModel(mateProductionPlanModel);
                }

                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateProductionPlan.getPlanId());
                mtAudit.setZjName(mateProductionPlan.getAuditName());
                mtAudit.setOrderType("7");
                mtAudit.setOrderNum(mateProductionPlan.getPlanOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);
    
            }

        } catch (Exception e) {

            mtProductionMapper.deleteMatePlan(mateProductionPlan.getPlanId());
            mtProductionMapper.deleteModelByPlanId(mateProductionPlan.getPlanId());
            resMap.put("orderId", mateProductionPlan.getPlanId());
            resMap.put("orderType", "7");
            mtAuditMapper.deleteMtAuditById(resMap);

        }

        return num;
    }

    @Override
    public List<MateProductionPlan> matePlanList(MateProductionPlan mateProductionPlan) {
        return mtProductionMapper.matePlanList(mateProductionPlan);
    }

    @Override
    public MateProductionPlan selectPlan(Long planId) {
        return mtProductionMapper.selectPlan(planId);
    }

    @Override
    public int editPlan(MateProductionPlan mateProductionPlan) {

        int num = 0;

        if (mateProductionPlan.getMtPlanModels() != null) {
            mtProductionMapper.deleteModelByPlanId(mateProductionPlan.getPlanId());
            for (MateProductionPlanModel mateProductionPlanModel : mateProductionPlan.getMtPlanModels()) {
                mateProductionPlanModel.setDeptId(SecurityUtils.getDeptId());
                mateProductionPlanModel.setPlanId(mateProductionPlan.getPlanId());
                mtProductionMapper.addMatePlanModel(mateProductionPlanModel);

            }
            num = mtProductionMapper.editPlan(mateProductionPlan);
        }
        return num;
    }

    @Override
    public void removePlan(Long planId) {

        mtProductionMapper.deleteMatePlan(planId);
        mtProductionMapper.deleteModelByPlanId(planId);
        Map<String, Object> resMap   = new HashMap<>();
        resMap.put("orderId", planId);
        resMap.put("orderType", "7");
        mtAuditMapper.deleteMtAuditById(resMap);

    }


    @Override
    public List<MaterialProduct> selectProductLists(MaterialProduct materialProduct) {
        return mtProductionMapper.selectProductLists(materialProduct);
    }

    @Override
    public List<ProductModel> selectTableList(Map<String, Object> resMap) {
        return mtProductionMapper.selectTableList(resMap);
    }

    @Override
    public List<ProductModel> selectProductList(Map<String, Object> resMap) {
        return mtProductionMapper.selectProductList(resMap);
    }
}
